package cn.edu.sjtu.se.dslab.component.design.generate;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import cn.edu.sjtu.se.dslab.component.aadl.AADLModel;
import cn.edu.sjtu.se.dslab.component.aadl.AADLParser;
import cn.edu.sjtu.se.dslab.component.dp.parse.DesignPatternInstanceBean;
import cn.edu.sjtu.se.dslab.project.Project;
import cn.edu.sjtu.se.dslab.rhapsody.entity.Agent;
import cn.edu.sjtu.se.dslab.rhapsody.entity.BusAgent;
import cn.edu.sjtu.se.dslab.rhapsody.entity.Task;
import cn.edu.sjtu.se.dslab.rhapsody.service.RhapsodyFileService;
import cn.edu.sjtu.se.dslab.util.Const;
import cn.edu.sjtu.se.dslab.util.FileUtil;

/**
 * 根据代理生成任务的函数
 * @author thinker
 *
 */
@Service("taskCodeGenerator")
public class TaskCodeGenerator  extends DesignPatternCodeGenerator {
	
	private static final Logger LOG = Logger.getLogger(TaskCodeGenerator.class);
	
	public static final String APPLICATION_C_FILE_NAME = "Application.c";
	public static final String APPLICATION_C_TEMPLATE_NAME = "Application_c.vm";
	
	@Resource(name="aadlParser")
	private AADLParser aadlParser;
	
	@Resource(name="rhapsodyFileService")
	private RhapsodyFileService rhapsodyFileService;
	
	public TaskCodeGenerator(){
		super();
	}

	@Override
	public void generateCode(String targetDirPath, Project project,
			DesignPatternInstanceBean... dpiBean) throws IOException {
		
//		AADLModel model = project.getModel();
//		LOG.info(model);
//		StringBuilder modelFilePath = new StringBuilder(FileUtil.getRootPath());
//		modelFilePath.append(Const.GENERATE_PATH).append("/").append(model.getXmlPath());
//		File modelFile = new File( modelFilePath.toString());
//		List<BusAgent> busAgents = rhapsodyFileService.readXmiBusAgents(project.getId());
//		List<Task> tasks = new ArrayList<Task>();
//		if (modelFile != null){
//			List<Set<Agent>> agents = aadlParser.parseAADLToAgentArrayList(modelFile);
//			int taskIndex = 1;
//			LOG.info(agents.size());
//			for (Set<Agent> agentSet: agents){
//				Task task  = new Task(taskIndex++);
//				for (Agent a: agentSet){
//					LOG.info(agentSet.size());
//					LOG.info(a.getType());
//					if(a.getType().equals(Agent.AGENT_BUS)){
//						for (BusAgent busAgent: busAgents){
//							if (a.getName().equals(busAgent.getName())){
//								task.addAgent(busAgent);   //查找原来的busAgent
//								break;
//							}
//						}
//					}else if (a.getType().equals(Agent.AGENT_CONTROL)){
//						task.addAgent(a);
//					}else if (a.getType().equals(Agent.AGENT_DISPLAY)){
//						task.addAgent(a);
//					}
//					LOG.info(a.getName() + "," + a.getType());
//				}
//				tasks.add(task);
//			}
//		}
//		
//		inputData("tasks", tasks);
//		StringBuffer applicationFilePath = new StringBuffer(targetDirPath);
//		applicationFilePath.append("App/").append(APPLICATION_C_FILE_NAME);
//		generateCodeFile(applicationFilePath.toString(), APPLICATION_C_TEMPLATE_NAME);
//		
		
		
		
		
	}
}
